<?php

namespace app\controller\uc;

use app\bll\Datadict as BllDatadict;
use app\bll\User as BllUser;
use app\bll\UserField as BllUserField;
use app\bll\UserTable as BllUserTable;
use core\controller\UcBase;
use Exception;
use support\View;

class Personal extends UcBase
{
    public function index()
    {
        //获取所有的信息表
        $bllUserTable = new BllUserTable();
        $infotables   = $bllUserTable->selectInfoTable();
        $bllUser      = new BllUser();
        $bllUserField = new BllUserField();

        $bllDatadict = new BllDatadict();
        $dicts = $bllDatadict->select();
        if ($dicts) {
            $dicts = array_column($dicts, null, 'id');
        }

        foreach ($infotables as $key => $infotable) {
            $fields = $bllUserField->select($infotable['name']);
            $infotables[$key]['dicts'] = $dicts;
            $infotables[$key]['fields'] = $fields;
            $infotables[$key]['infomation'] = $bllUser->userInfomation($infotable, request()->userinfo['id']);
        }

        View::assign('infotables', $infotables);
        return $this->view();
    }

    public function addInfomation()
    {
        $tablename = request()->get('tablename');
        if (is_post()) {
            try {
                $bllUser = new BllUser();
                $data = request()->post();
                $data['uid'] = request()->userinfo['id'];
                $bllUser->addUserInfomation($data, $tablename);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $bllUserField = new BllUserField();
            $fields       = $bllUserField->select($tablename);

            View::assign('fields', $fields);
            return $this->view(frame: false);
        }
    }

    /**
     * 修改信息
     *
     * @return mixed
     */
    public function editInfomation()
    {
        $tablename = request()->get('tablename');
        $id        = request()->get('id');
        $bllUser   = new BllUser();
        if (is_post()) {
            try {

                $data = request()->post();
                $bllUser->editUserInfomation(
                    request()->userinfo['id'],
                    $data,
                    $tablename,
                    request()->get('id', null)
                );
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $bllUserField = new BllUserField();
            $fields       = $bllUserField->select($tablename);

            $infomation = $bllUser->findInfomationByUid($tablename, $id, request()->userinfo['id']);
            View::assign('fields', $fields);
            View::assign('infomation', $infomation);

            return $this->view(frame: false);
        }
    }

    public function delInfomation()
    {
        $tablename = request()->get('tablename');
        try {
            $bllUser = new BllUser();
            $data = request()->post();
            $bllUser->delUserInfomation(
                request()->get('id'),
                request()->userinfo['id'],
                $tablename
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    public function changePwd()
    {
        if (is_post()) {

            try {
                $data = request()->post();
                $bllUser = new BllUser();
                $bllUser->changePwd(
                    $data['oldpwd'],
                    $data['newpwd'],
                    $data['renewpwd'],
                    request()->userinfo['id']
                );
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view(frame: false);
        }
    }
}
